package cn.codeartist.wechat.qywx.entity.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

/**
 * jnai
 *
 * @author 艾江南
 * @date 2020/7/28
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class R<T> {
    /**
     * 错误编码
     */
    private int code;
    /**
     * 错误消息
     */
    private String message;
    /**
     * 数据
     */
    private T data;

    public R(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static R<?> ok() {
        return of(HttpStatus.OK);
    }

    public static <T> R<T> ok(T data) {
        return of(HttpStatus.OK, data);
    }

    public static R<?> badRequest() {
        return of(HttpStatus.BAD_REQUEST);
    }

    public static <T> R<T> badRequest(T data) {
        return of(HttpStatus.BAD_REQUEST, data);
    }

    public static R<?> error() {
        return of(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    public static <T> R<T> error(T data) {
        return of(HttpStatus.INTERNAL_SERVER_ERROR, data);
    }

    public static R<?> of(HttpStatus httpStatus) {
        return new R<>(httpStatus.value(), httpStatus.getReasonPhrase());
    }

    public static <T> R<T> of(HttpStatus httpStatus, T data) {
        return new R<>(httpStatus.value(), httpStatus.getReasonPhrase(), data);
    }
}
